home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MSG Demo 1.2 Source / MSG Demo ƒ / MSG Shell ƒ / msg prefs.c < prev    next >
Text File  |  1993-11-10  |  10KB  |  425 lines

  1. /**********************************************************************\
  2.  
  3. File:        msg prefs.c
  4.  
  5. Purpose:    This module handles creating/opening/closing/updating
  6.             the preference file, and copying the preference file
  7.             data into application globals (and back).
  8.  
  9.  
  10. MSG Demo -- graphic effects demonstration program
  11. Copyright (C) 1992-3 Mark Pilgrim & Dave Blumenthal
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "msg prefs.h"
  31. #include "msg dialogs.h"
  32. #include "msg graphics.h"
  33. #include "msg environment.h"
  34. #include "msg sounds.h"
  35. #include "util.h"
  36.  
  37. long                gFileID;
  38. Str255                gMyName;
  39. Str255                gMyOrg;
  40. Boolean                gCanSavePrefs;
  41.  
  42. static PrefStruct    thePrefs;
  43. static long            gPrefsFilePos;
  44.  
  45. int PreferencesInit(void)
  46. {
  47.     int            prefsFileID;
  48.     int            err;
  49.     
  50.     gCanSavePrefs=FALSE;
  51.     err=GetFileID();
  52.     if (err!=prefs_allsWell)
  53.         return err;
  54.     
  55.     err=OpenPrefsFile(&prefsFileID);
  56.     if (err!=prefs_allsWell)
  57.     {
  58.         if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
  59.             ClosePrefsFile(prefsFileID);
  60.         return err;
  61.     }
  62.     
  63.     err=CheckVersion(prefsFileID);
  64.     if (err!=prefs_allsWell)
  65.     {
  66.         ClosePrefsFile(prefsFileID);
  67.         return err;
  68.     }
  69.     
  70.     GetFPos(prefsFileID, &gPrefsFilePos);
  71.     gPrefsFilePos-=sizeof(thePrefs);
  72.     do
  73.     {
  74.         gPrefsFilePos+=sizeof(thePrefs);
  75.         err=GetNextPrefs(prefsFileID);
  76.         if (err==prefs_noMorePrefsErr)
  77.             return (Virgin(prefsFileID));
  78.         
  79.         if (err!=prefs_allsWell)
  80.         {
  81.             ClosePrefsFile(prefsFileID);
  82.             return err;
  83.         }
  84.         
  85.         err=CheckFileID();
  86.     }
  87.     while (err==prefs_IDNotMatchErr);
  88.     
  89.     CopyPrefsToGlobals();
  90.     ClosePrefsFile(prefsFileID);
  91.     
  92.     return prefs_allsWell;
  93. }
  94.  
  95. void PrefsError(int err)
  96. {
  97.     switch (err)
  98.     {
  99.         case prefs_diskReadErr:
  100.             ParamText("\pThe MSG Demo preferences file could not be accessed, because a \
  101. disk read error occurred.  Default preferences will be used for now, but the file may \
  102. be damaged, and you should probably throw it in the trash.", "\p", "\p", "\p");
  103.             break;
  104.         case prefs_diskWriteErr:
  105.             ParamText("\pThe MSG Demo preferences file could not be accessed, because a \
  106. disk write error occurred.  The file may be damaged, and you should probably throw it \
  107. in the trash.","\p","\p","\p");
  108.             break;
  109.         case prefs_cantCreatePrefsErr:
  110.             ParamText("\pThe MSG Demo preferences file could not be created.  The \
  111. disk may be locked or full.  Default preferences will be used for now.","\p","\p","\p");
  112.             break;
  113.         case prefs_cantOpenPrefsErr:
  114.             ParamText("\pThe MSG Demo preferences file could not be accessed.  The \
  115. file may be damaged, and you should probably throw it in the trash.  Default preferences \
  116. will be used for now.","\p","\p","\p");
  117.             break;
  118.         case prefs_versionNotSupportedErr:
  119.             ParamText("\pThe preferences file you have in your system folder was created \
  120. by a later version of MSG Demo than this copy.  If you want to be able to save your \
  121. preferences with this copy, you will have to throw the current preferences file in the \
  122. trash.","\p","\p","\p");
  123.             break;
  124.     }
  125.     switch (err)
  126.     {
  127.         case prefs_diskReadErr:
  128.         case prefs_diskWriteErr:
  129.         case prefs_cantCreatePrefsErr:
  130.         case prefs_cantOpenPrefsErr:
  131.         case prefs_versionNotSupportedErr:
  132.             gCanSavePrefs=FALSE;
  133.             PositionDialog('ALRT', generalAlert);
  134.             StopAlert(generalAlert, 0L);
  135.             break;
  136.         default:
  137.         {
  138.             gCanSavePrefs=TRUE;
  139.         }
  140.     }
  141. }
  142.  
  143. int OpenPrefsFile(int *prefsFileID)
  144. {
  145.     int                thisFile;
  146.     OSErr            isHuman;
  147.     int                vRefNum;
  148.     long            dirID;
  149.     FSSpec            prefsFile;
  150.     FInfo            prefsInfo;
  151.     Boolean            newPrefs;
  152.     unsigned char    *name=PREFS_FILE_NAME;
  153.     
  154.     newPrefs=FALSE;
  155.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  156.     
  157.     if (isHuman!=noErr)
  158.         return prefs_cantOpenPrefsErr;
  159.     if (gHasFSSpecs)
  160.     {
  161.         isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
  162.         if (isHuman!=noErr)
  163.         {
  164.             if (isHuman==fnfErr)
  165.             {
  166.                 isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
  167.                 if (isHuman!=noErr)
  168.                     return prefs_cantCreatePrefsErr;
  169.                 newPrefs=TRUE;
  170.             }
  171.             else return prefs_cantOpenPrefsErr;
  172.         }
  173.         isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
  174.         *prefsFileID=thisFile;
  175.         if (isHuman!=noErr)
  176.             return prefs_cantOpenPrefsErr;
  177.     }
  178.     else
  179.     {
  180.         isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  181.         *prefsFileID=thisFile;
  182.         if (isHuman!=noErr)
  183.         {
  184.             if (isHuman==fnfErr)
  185.             {
  186.                 isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
  187.                 if (isHuman!=noErr)
  188.                     return prefs_cantCreatePrefsErr;
  189.                 prefsInfo.fdType=PREFS_TYPE;
  190.                 prefsInfo.fdCreator=CREATOR;
  191.                 prefsInfo.fdFlags=0;
  192.                 prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
  193.                 prefsInfo.fdFldr=0;
  194.                 isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
  195.                 if (isHuman!=noErr)
  196.                     return prefs_cantCreatePrefsErr;
  197.                 isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  198.                 *prefsFileID=thisFile;
  199.                 if (isHuman!=noErr)
  200.                     return prefs_cantOpenPrefsErr;
  201.                 newPrefs=TRUE;
  202.             }
  203.             else return prefs_cantOpenPrefsErr;
  204.         }
  205.     }
  206.     if (newPrefs)
  207.         return SetupNewPrefsFile(*prefsFileID);
  208.     
  209.     return prefs_allsWell;
  210. }
  211.  
  212. int SetupNewPrefsFile(int prefsFileID)
  213. {
  214.     int                err;
  215.     OSErr            isHuman;
  216.     long            count;
  217.     int                temp;
  218.     
  219.     gPrefsFilePos=2L;
  220.     isHuman=SetEOF(prefsFileID, 2L);
  221.     if (isHuman!=noErr)
  222.         return prefs_diskWriteErr;
  223.     SetFPos(prefsFileID, 1, 0L);
  224.     temp=PREFS_HEADER_VERSION;
  225.     count=2L;
  226.     isHuman=FSWrite(prefsFileID, &count, &temp);
  227.     if (isHuman!=noErr)
  228.         return prefs_diskWriteErr;        
  229.     err=Virgin(prefsFileID);
  230.     return err;
  231. }
  232.  
  233. void ClosePrefsFile(int prefsFileID)
  234. {
  235.     FSClose(prefsFileID);
  236.     FlushVol(0L, kOnSystemDisk);
  237. }
  238.  
  239. int GetNextPrefs(int prefsFileID)
  240. {
  241.     OSErr        isHuman;
  242.     long        count;
  243.     
  244.     count=sizeof(thePrefs);
  245.     isHuman=FSRead(prefsFileID, &count, &thePrefs);
  246.     if (isHuman==eofErr)
  247.         return prefs_noMorePrefsErr;
  248.     if (isHuman!=noErr)
  249.         return prefs_diskReadErr;
  250.     
  251.     return prefs_allsWell;
  252. }
  253.  
  254. int SavePrefs(int prefsFileID)
  255. {
  256.     long        oldEOF;
  257.     OSErr        isHuman;
  258.     long        count;
  259.     
  260.     GetEOF(prefsFileID, &oldEOF);
  261.     if (gPrefsFilePos>=oldEOF)
  262.     {
  263.         isHuman=SetEOF(prefsFileID, oldEOF+sizeof(thePrefs));
  264.         if (isHuman!=noErr)
  265.             return prefs_diskWriteErr;
  266.     }
  267.     SetFPos(prefsFileID, 1, gPrefsFilePos);
  268.     count=sizeof(thePrefs);
  269.     isHuman=FSWrite(prefsFileID, &count, &thePrefs);
  270.     if (isHuman!=noErr)
  271.         return prefs_diskWriteErr;
  272.     
  273.     return prefs_allsWell;
  274. }
  275.  
  276. int CheckVersion(int prefsFileID)
  277. {
  278.     OSErr        isHuman;
  279.     long        count;
  280.     int            temp;
  281.     
  282.     count=2L;
  283.     isHuman=FSRead(prefsFileID, &count, &temp);
  284.     if (isHuman!=noErr)
  285.         return prefs_diskReadErr;
  286.     if (temp>PREFS_HEADER_VERSION)
  287.         return prefs_versionNotSupportedErr;
  288.     if (temp<PREFS_HEADER_VERSION)
  289.         return SetupNewPrefsFile(prefsFileID);
  290.     
  291.     return prefs_allsWell;
  292. }
  293.  
  294. int GetFileID(void)
  295. {
  296.     CInfoPBRec        pb;
  297.     int                err;
  298.     
  299.     pb.hFileInfo.ioCompletion=0L;
  300.     pb.hFileInfo.ioNamePtr=CurApName;
  301.     pb.hFileInfo.ioVRefNum=0;
  302.     pb.hFileInfo.ioFDirIndex=0;
  303.     pb.hFileInfo.ioDirID=0;
  304.     err=PBGetCatInfo(&pb, FALSE);
  305.     if (err!=noErr)
  306.         return prefs_diskReadErr;
  307.     
  308.     gFileID=pb.hFileInfo.ioDirID;
  309.     
  310.     return prefs_allsWell;
  311. }
  312.  
  313. int CheckFileID(void)
  314. {
  315.     return (thePrefs.fileID==gFileID) ? prefs_allsWell : prefs_IDNotMatchErr;
  316. }
  317.  
  318. int Virgin(int prefsFileID)
  319. {
  320.     int            err;
  321.     
  322.     DefaultPrefs();
  323.     CopyGlobalsToPrefs();
  324.     err=SavePrefs(prefsFileID);
  325.     if (err!=prefs_allsWell)
  326.         return err;
  327.     DoSound(sound_virgin, TRUE);
  328.     GetRegistration();
  329.     CopyGlobalsToPrefs();
  330.     err=SavePrefs(prefsFileID);
  331.     
  332.     return (err==prefs_allsWell) ? prefs_virginErr : err;
  333. }
  334.  
  335. void DefaultPrefs(void)
  336. {
  337.     gMyName[0]=0x03;
  338.     gMyName[1]='B';
  339.     gMyName[2]='o';
  340.     gMyName[3]='b';
  341.     gMyOrg[0]=0x00;
  342.     gSoundToggle=0xff;
  343. }
  344.  
  345. void CopyGlobalsToPrefs(void)
  346. {
  347.     Mymemset(&thePrefs, 0, sizeof(thePrefs));
  348.     if (gMyName[0]>0x27)
  349.         gMyName[0]=0x27;
  350.     if (gMyOrg[0]>0x27)
  351.         gMyOrg[0]=0x27;
  352.     Mymemcpy(thePrefs.regname, gMyName, gMyName[0]+1);
  353.     Mymemcpy(thePrefs.regorg, gMyOrg, gMyOrg[0]+1);
  354.     thePrefs.sound=gSoundToggle;
  355.     thePrefs.fileID=gFileID;
  356. }
  357.  
  358. void CopyPrefsToGlobals(void)
  359. {
  360.     Mymemcpy(gMyName, thePrefs.regname, thePrefs.regname[0]+1);
  361.     Mymemcpy(gMyOrg, thePrefs.regorg, thePrefs.regorg[0]+1);
  362.     gSoundToggle=thePrefs.sound;
  363. }
  364.  
  365. void GetRegistration(void)
  366. {
  367.     DialogPtr        theDlog;
  368.     int                itemSelected = 0;
  369.     int                newleft;
  370.     int                newtop;
  371.     int                itemType;
  372.     Handle            item;
  373.     Rect            box;
  374.     int                dlogID;
  375.     
  376.     if(GetWindowDepth() == 1)
  377.         dlogID = bwPersonalDialog;
  378.     else
  379.         dlogID = colorPersonalDialog;
  380.         
  381.     theDlog = GetNewDialog(dlogID, 0L, (WindowPtr)-1L);
  382.     newleft = gMainScreenBounds.left + (((gMainScreenBounds.right -
  383.                 gMainScreenBounds.left) - (theDlog->portRect.right -
  384.                 theDlog->portRect.left)) / 2);
  385.     newtop = gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  386.                 gMainScreenBounds.top) - (theDlog->portRect.bottom -
  387.                 theDlog->portRect.top)) / 2);
  388.     if(newtop < 15)
  389.         newtop = 15;
  390.     GetDItem(theDlog, 1, &itemType, &item, &box);
  391.     InsetRect(&box, -4, -4);
  392.     SetDItem(theDlog, 8, userItem, OutlineDefaultButton, &box);
  393.  
  394.     MoveWindow(theDlog, newleft, newtop, TRUE);
  395.     ShowWindow(theDlog);
  396.     while(itemSelected != 1)
  397.     {
  398.         ModalDialog(0L, &itemSelected);
  399.     }
  400.     GetDItem(theDlog,4,&itemType,&item,&box);
  401.     GetIText(item,&gMyName);
  402.     
  403.     GetDItem(theDlog,5,&itemType,&item,&box);
  404.     GetIText(item,&gMyOrg);
  405.  
  406.     if (gMyName[0]=='\0')
  407.         DefaultPrefs();
  408.  
  409.     HideWindow(theDlog);
  410.     DisposeDialog(theDlog);
  411. }
  412.  
  413. void SaveThePrefs(void)
  414. {
  415.     int            prefsFileID;
  416.     
  417.     if (gCanSavePrefs)
  418.     {
  419.         OpenPrefsFile(&prefsFileID);
  420.         CopyGlobalsToPrefs();
  421.         SavePrefs(prefsFileID);
  422.         ClosePrefsFile(prefsFileID);
  423.     }
  424. }
  425.